local skel = fk.CreateSkill {
  name = "emo__lingshu",
}

Fk:loadTranslationTable{
  ["emo__lingshu"] = "灵枢",
  [":emo__lingshu"] = "你可以视为使用①【酒】/②【火攻】/③【无懈可击】并摸X张牌（X为此牌序号数），序号数不大于你手牌中有以此法摸的牌张数的项失效。",

  ["#emo__lingshu"] = "灵枢：转化一张牌",
  ["@@emo__lingshu-inhand"] = "灵枢",
}

---@param player Player
local getVSNames = function (player)
  local names = {"analeptic", "fire_attack", "nullification"}
  local num = #table.filter(player:getCardIds("h"), function (id)
    return Fk:getCardById(id):getMark("@@emo__lingshu-inhand") ~= 0
  end)
  if num >= 3 then return {} end
  return table.slice(names, num + 1, 5)
end

skel:addEffect("viewas", {
  prompt = "#emo__lingshu",
  mute_card = false,
  pattern = "analeptic,fire_attack,nullification",
  interaction = function(self, player)
    local all_choices = getVSNames(player)
    local choices = player:getViewAsCardNames(skel.name, all_choices)
    if #choices > 0 then
      return UI.ComboBox { choices = choices, all_choices = all_choices }
    end
  end,
  card_filter = Util.FalseFunc,
  view_as = function (self, player, cards)
    if Fk.all_card_types[self.interaction.data] == nil then return nil end
    local c = Fk:cloneCard(self.interaction.data)
    c.skillName = skel.name
    return c
  end,
  before_use = function(self, player, use)
    local names = { "analeptic", "fire_attack", "nullification" }
    local index = table.indexOf(names, use.card.name)
    if index == -1 then return "" end
    use.extra_data = use.extra_data or {}
    use.extra_data.emo__lingshu_draw = index
  end,
  enabled_at_play = function(self, player)
    return #player:getViewAsCardNames(skel.name, getVSNames(player)) > 0
  end,
  enabled_at_response = function (self, player, response)
    if not response and Fk.currentResponsePattern then
      return #player:getViewAsCardNames(skel.name, getVSNames(player)) > 0
    end
  end,
})

skel:addEffect(fk.CardUsing, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    if target == player and not player.dead then
      return data.extra_data and data.extra_data.emo__lingshu_draw
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player:drawCards(data.extra_data.emo__lingshu_draw, skel.name, nil, "@@emo__lingshu-inhand")
  end,
})

return skel
